<?
/*
$array = array(0 => 100, "color" => "red");
print_r(array_keys($array));

$array = array("blue", "red", "green", "blue", "blue");
print_r(array_keys($array, "blue"));

$array = array("color" => array("blue", "red", "green"),
               "size"  => array("small", "medium", "large"));
print_r(array_keys($array));

exit();        
*/
require_once("Export2ExcelClass.php");

$excel = new Export2ExcelClass; 

$consulta="";
$consulta=$consulta."SELECT s.nom_sorteo AS \"NUMERO_SORTEO\", l.nlocal as \"LOCAL\", sum(f.val_fact) as \"VALOR_FACTURADO\" FROM sorteos s, facturas f, locales l WHERE s.is_sorteo = f.id_sort AND l.id_loc = f.id_empresa group by s.nom_sorteo , l.nlocal";

/**** POSTGRES
//--- obtengo la conexion
if (!($link=pg_connect("host=190.27.248.70 port=5432 dbname=ServiPortuarios user=postgres password=123456")))
{ 
	echo json_encode(array("ERROR"=>"Error conectando a la base de datos."));  
	exit(); 
}
$res= pg_query($link,$consulta);
$Matriz=pg_fetch_all($res);      

 */

//$newArray = array_keys($Matriz[0]);
//foreach ($newArray as $key => $value) {
//        echo "$key - <strong>$value</strong> <br />"; 
//}
//exit();

/**MYSQL*/


$conect = mysql_connect("localhost","hitechco_fgtecno","Since1928+");
mysql_select_db(DB_NAME,$conect);

@mysql_query("SET NAMES 'utf8'");
//connexion 
if (!$conect) {
    echo "La conexion no se pudo realizar, consulte con el administrador del sistema.";
    //exit;
}else{
    //seleccion de la base de datos
    $db = mysql_select_db("hitechco_moad", $conect);
    if (!$db) {
            echo "La base de datos no esta en linea, comuniquese con el administrador del sistema";
            //exit;
    }
}

//$result_pag_data = mysql_query($consulta) or die('MySql Error: ' . mysql_error());

$res= mysql_query($consulta);
$Matriz=$excel->mysql_fetch_all($res);      

//while ($row = mysql_fetch_array($res)) {
//    echo $row[0]." - ";
//}

//foreach($Matriz as $Row){ 
//    foreach($Row as $Value){ 
//        echo $Value.' | '; 
//    } 
//    echo '<br/>';
//} 
//
//echo "ok...";
//exit();

//Convertimos la matriz a Excel: 
$excel->WriteMatriz($Matriz); 

//Hacemos que sea descargable: 
$excel->Download("ReporteLocales"); 

mysql_close($conect);

?>